package com.glory.dgcpcpda.adapter;

import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.MyApp;
import com.glory.dgcpcpda.entity.QCformEntity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class DataLinesAdapter extends BaseQuickAdapter<QCformEntity.QCFORMINFO.DATALINESINFO, BaseViewHolder> {

    private BigDecimal datanums;
    public DataLinesAdapter(@Nullable ArrayList<QCformEntity.QCFORMINFO.DATALINESINFO> data,BigDecimal datanums) {
        super(R.layout.item_data_lines, data);
        this.datanums=datanums;
    }

    @Override
    protected void convert(BaseViewHolder helper, QCformEntity.QCFORMINFO.DATALINESINFO item) {
        helper.setText(R.id.tvItemName,item.getITEMNAME());
        helper.setText(R.id.tvItemDesc,item.getITEMDESC());
        helper.setText(R.id.tvDataType,item.getDATATYPE());
        helper.setText(R.id.tvMinValue,item.getMINVALUE());
        helper.setText(R.id.tvMaxValue,item.getMAXVALUE());
        helper.setText(R.id.etResultFlag,item.getRESULTFLAG());
        helper.setText(R.id.etResulta,item.getDATAA());
        helper.setText(R.id.etResultb,item.getDATAB());
        helper.setText(R.id.etResultc,item.getDATAC());


        /*if (item.getDATATYPE().equals("NUM")){
            etResult.setInputType(InputType.TYPE_CLASS_TEXT);
            etResult.setRawInputType(InputType.TYPE_CLASS_NUMBER);
        }*/
        EditText etResulta = helper.getView(R.id.etResulta);
        EditText etResultb = helper.getView(R.id.etResultb);
        EditText etResultc = helper.getView(R.id.etResultc);
        EditText etResultFlag = helper.getView(R.id.etResultFlag);
        if(datanums.compareTo(BigDecimal.valueOf(3))==0){
            etResultc.setVisibility(View.VISIBLE);
            etResultb.setVisibility(View.VISIBLE);
        }else if(datanums.compareTo(BigDecimal.valueOf(2))==0){
            etResultc.setVisibility(View.GONE);
            etResultb.setVisibility(View.VISIBLE);
        }else{
            etResultb.setVisibility(View.GONE);
            etResultc.setVisibility(View.GONE);
        }
        setEditListenera(etResulta,etResultb,etResultc,etResultFlag,item);
        setEditListenerb(etResulta,etResultb,etResultc,etResultFlag,item);
        setEditListenerc(etResulta,etResultb,etResultc,etResultFlag,item);
        setEditListenerResultFlag(etResultFlag,etResulta,item);
    }

    private void setEditListenera(EditText editTexta,EditText editTextb,EditText editTextc,EditText etResultFlag, QCformEntity.QCFORMINFO.DATALINESINFO item) {
        if (editTexta.getTag() instanceof TextWatcher) {
            editTexta.removeTextChangedListener((TextWatcher) editTexta.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    item.setDATAA(s.toString());
                    item.setDATAB(editTextb.getText().toString());
                    item.setDATAC(editTextc.getText().toString());
                    item.setRESULTFLAG(etResultFlag.getText().toString());
                    int value = Integer.parseInt(s.toString());
                   /* if (value >= Integer.parseInt(item.getMINVALUE()) && value <= Integer.parseInt(item.getMAXVALUE())){
                        editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                    }else {
                        editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                    }*/
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        editTexta.addTextChangedListener(watcher);
        editTexta.setTag(watcher);
    }
    private void setEditListenerb(EditText editTexta,EditText editTextb,EditText editTextc,EditText etResultFlag, QCformEntity.QCFORMINFO.DATALINESINFO item) {
        if (editTextb.getTag() instanceof TextWatcher) {
            editTextb.removeTextChangedListener((TextWatcher) editTextb.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    item.setDATAB(s.toString());
                    item.setDATAA(editTexta.getText().toString());
                    item.setDATAC(editTextc.getText().toString());
                    item.setRESULTFLAG(etResultFlag.getText().toString());
                    int value = Integer.parseInt(s.toString());
                   /* if (value >= Integer.parseInt(item.getMINVALUE()) && value <= Integer.parseInt(item.getMAXVALUE())){
                        editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                    }else {
                        editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                    }*/
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        editTextb.addTextChangedListener(watcher);
        editTextb.setTag(watcher);
    }
    private void setEditListenerc(EditText editTexta,EditText editTextb,EditText editTextc,EditText etResultFlag, QCformEntity.QCFORMINFO.DATALINESINFO item) {
        if (editTextc.getTag() instanceof TextWatcher) {
            editTextc.removeTextChangedListener((TextWatcher) editTextc.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    item.setDATAC(s.toString());
                    item.setDATAA(editTexta.getText().toString());
                    item.setDATAB(editTextb.getText().toString());
                    item.setRESULTFLAG(etResultFlag.getText().toString());
                    int value = Integer.parseInt(s.toString());
                   /* if (value >= Integer.parseInt(item.getMINVALUE()) && value <= Integer.parseInt(item.getMAXVALUE())){
                        editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                    }else {
                        editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                    }*/
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        editTextc.addTextChangedListener(watcher);
        editTextc.setTag(watcher);
    }
    private void setEditListenerResultFlag(EditText editText,EditText editTexta, QCformEntity.QCFORMINFO.DATALINESINFO item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    item.setRESULTFLAG(s.toString());
                    item.setDATAA(editTexta.getText().toString());

                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
